package com.study.workflow;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author sjw
 * @description
 * @date 2019-08-23 上午8:21
 */
public class QjHoliday {
    public static void main(String[] args) {

        ProcessEngine engine = ProcessEngineFactory.processEngine();
        ManagementService managementService= engine.getManagementService();
        managementService.
        RepositoryService repositoryService = engine.getRepositoryService();
        // Deployment deployment = repositoryService.createDeployment().addClasspathResource("qj-bpmn20.xml").deploy();
        //System.out.println(deployment.getId());
//        Deployment deployment = repositoryService.createDeployment().addClasspathResource("aaa-bpmn20.xml").deploy();
//        System.out.println(deployment.getId());
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId("25001")
                //.deploymentId(deployment.getId())
                .singleResult();
        System.out.println("Found process definition : " + processDefinition.getName());

        Scanner scanner = new Scanner(System.in);
        System.out.println("define:0 start:1 approve:2");
        Integer step = 1;
        Integer a = Integer.valueOf(scanner.nextLine());
        if (step.equals(a)) {

            System.out.println("Who are you?");
            String employee = scanner.nextLine();

            System.out.println("How many holidays do you want to request?");
            Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());

            System.out.println("Why do you need them?");
            String description = scanner.nextLine();

            RuntimeService runtimeService = engine.getRuntimeService();

            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("employee", employee);
            variables.put("nrOfHolidays", nrOfHolidays);
            variables.put("description", description);
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("aaa", variables);
        } else {

            Map<String, Object> variables1 = new HashMap<String, Object>();
            TaskService taskService = engine.getTaskService();
            List<Task> tasks = taskService.createTaskQuery().list();
//        List<Task> tasks = taskService.createTaskQuery().taskVariableValueEquals("nrOfHolidays","12").list();

            System.out.println("You have " + tasks.size() + " tasks:");
            for (int i = 10; i < tasks.size(); i++) {
                System.out.println((i + 1) + ") " + tasks.get(i).getName() + "  taskId " + tasks.get(i).getId());
            }
            if (tasks.size() > 0) {
                System.out.println("Which task would you like to complete?");
                int taskIndex = Integer.valueOf(scanner.nextLine());
                Task task = tasks.get(taskIndex - 1);
                Map<String, Object> processVariables = taskService.getVariables(task.getId());
                System.out.println(processVariables.get("employee") + " wants " +
                        processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");
                boolean approved = scanner.nextLine().toLowerCase().equals("y");
                variables1 = new HashMap<String, Object>();
                variables1.put("approved", approved);
                taskService.complete(task.getId(), variables1);
            }
        }

    }
}
